package com.lab.controller.user;

import com.lab.common.BusinessException;
import com.lab.common.Result;
import com.lab.dto.WeeklyReportContentDTO;
import com.lab.dto.WeeklyReportDTO;
import com.lab.dto.WeeklyReportSubmissionDTO;
import com.lab.entity.Member;
import com.lab.service.MemberService;
import com.lab.service.WeeklyReportService;
import com.lab.vo.WeeklyReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 学生端-周报管理：只查询当前登录学生自己的周报（包含内容）
 */
@RestController
@RequestMapping("/student/weeklyReport")
@Api(tags = "郭帅斐学生端-周报管理")
@Slf4j
public class WeeklyReportStudentController {

    private final WeeklyReportService weeklyReportService;
    private final MemberService memberService;

    public WeeklyReportStudentController(WeeklyReportService weeklyReportService, MemberService memberService) {
        this.weeklyReportService = weeklyReportService;
        this.memberService = memberService;
    }

    /**
     * 学生分页查询自己的周报详情（包含内容列表）
     */
    @GetMapping("/page")
    @ApiOperation("学生分页查询自己的周报详情")
    public Result<List<WeeklyReportVO>> pageMyReports(
            @ApiParam(value = "页码（从1开始）", required = true, example = "1")
            @RequestParam Integer page,
            @ApiParam(value = "每页条数（1-100）", required = true, example = "10")
            @RequestParam Integer pageSize,
            HttpServletRequest request) {

        // LoginInterceptor 已在通过 JWT 校验后注入 userId
        Integer userId = (Integer) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或令牌无效");
        }

        log.info("学生分页查询自己的周报：userId={}, page={}, pageSize={}", userId, page, pageSize);
        List<WeeklyReportVO> list = weeklyReportService.getReportWithContentsByMemberIdAndPage(userId, page, pageSize);
        return Result.success(list);
    }

    /**
     * 学生提交周报
     */
    @PostMapping("/submit")
    @ApiOperation("学生提交周报")
    public Result<String> submitWeeklyReport(
            @RequestBody WeeklyReportSubmissionDTO submissionDTO,
            HttpServletRequest request) {

        Integer userId = (Integer) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或令牌无效");
        }

        WeeklyReportDTO weeklyReportDTO = submissionDTO.getWeeklyReportDTO();
        WeeklyReportContentDTO weeklyReportContentDTO = submissionDTO.getWeeklyReportContentDTO();

        // 根据username获取memberId
        Member member = memberService.getByNameSingle(weeklyReportDTO.getUsername());
        if (member == null) {
            throw new BusinessException(400, "用户不存在");
        }
        Integer memberIdFromUsername = member.getId();

        // 验证当前登录用户ID与周报提交用户ID是否一致
        if (!userId.equals(memberIdFromUsername)) {
            throw new BusinessException(400, "无权提交他人周报");
        }

        // 调用服务层方法添加周报
        weeklyReportService.addWeeklyReportWithContent(memberIdFromUsername, weeklyReportDTO, weeklyReportContentDTO);
        return Result.success("周报提交成功");
    }
}